package hos.ui.item;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RadioButton;

import java.util.List;
import java.util.Map;

import hos.java.util.AnyUtils;
import hos.ui.R;
import hos.ui.radio.RadioGroupExtend;

/**
 * <p>Title: ItemTextLayout </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/6/9 17:45
 */
public class ItemRadioLayout extends ItemTitleLayout {

    private RadioGroupExtend radioGroup;

    public ItemRadioLayout(Context context) {
        this(context, null);
    }

    public ItemRadioLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemRadioLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void initAttributeSet(Context context, AttributeSet attrs, int defStyleAttr) {
        @SuppressWarnings("resource") TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.ItemRadioLayout);
        try {
            final CharSequence[] entries = typedArray.getTextArray(R.styleable.ItemRadioLayout_android_entries);
            int index = typedArray.getInt(R.styleable.ItemRadioLayout_childOrientation, -1);
            radioGroup = genRadioGroup(attrs);
            radioGroup.addArray(entries);
            if (index >= 0) {
                radioGroup.setOrientation(index);
            }
            int orientation = getOrientation();
            if (orientation == HORIZONTAL) {
                int childGravity = typedArray.getInt(R.styleable.ItemRadioLayout_childGravity, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                radioGroup.setGravity(childGravity);
                LayoutParams layoutParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1;
                radioGroup.setLayoutParams(layoutParams);
            } else {
                int childGravity = typedArray.getInt(R.styleable.ItemRadioLayout_childGravity, Gravity.LEFT | Gravity.CENTER_VERTICAL);
                radioGroup.setGravity(childGravity);
                radioGroup.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
            int childMinHeight = typedArray.getDimensionPixelOffset(R.styleable.ItemRadioLayout_childMinHeight, dp2px(48));
            radioGroup.setMinimumHeight(childMinHeight);
            addView(radioGroup);
        } finally {
            typedArray.recycle();
        }
    }

    @Override
    public void setContent(Map<String, Object> map, String key) {
        setKey(key);
        if (radioGroup == null) {
            return;
        }
        if (!map.containsKey(key)) {
            return;
        }
        Object value = map.get(key);
        if (value == null) {
            radioGroup.clearCheck();
            return;
        }
        radioGroup.checkText(AnyUtils.empty(value));
    }

    @Override
    public void setContent(CharSequence content) {
        if (radioGroup == null) {
            return;
        }
        radioGroup.checkText(content);
    }

    public String getText() {
        if (radioGroup == null) {
            return "";
        }
        return AnyUtils.empty(radioGroup.getCheckText());
    }

    @Override
    public void clear() {
        if (radioGroup == null) {
            return;
        }
        radioGroup.clearCheck();
    }


    protected RadioGroupExtend genRadioGroup(AttributeSet attrs) {
        return new RadioGroupExtend(getContext(),attrs);
    }

    public RadioGroupExtend getRadioGroup() {
        return radioGroup;
    }


    public void checkPosition(int position) {
        if (radioGroup == null) {
            return;
        }
        radioGroup.checkPosition(position);
    }

    public void checkText(CharSequence text) {
        if (radioGroup == null) {
            return;
        }
        radioGroup.checkText(text);
    }


    public RadioButton getCheckRadio() {
        if (radioGroup == null) {
            return null;
        }
        return radioGroup.getCheckRadio();
    }

    public int getCheckPosition() {
        if (radioGroup == null) {
            return -1;
        }
        return radioGroup.getCheckPosition();
    }

    public void addList(List<CharSequence> textList) {
        addArray(textList.toArray(new CharSequence[0]));
    }

    public void addArray(CharSequence[] textList) {
        add(textList);
    }

    public void add(CharSequence... textList) {
        if (radioGroup == null) {
            return;
        }
        radioGroup.add(textList);
    }

    public void setOnCheckedListener(RadioGroupExtend.OnCheckedListener listener) {
        if (radioGroup == null) {
            return;
        }
        radioGroup.setOnCheckedListener(listener);
    }

    public void setContentEnabled(boolean enabled) {
        if (radioGroup != null) {
            radioGroup.setEnabled(enabled);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        if (radioGroup != null) {
            radioGroup.setEnabled(enabled);
        }
        super.setEnabled(enabled);
    }

    public void setClickableEnabled(boolean clickable) {
        if (radioGroup != null) {
            radioGroup.setClickable(clickable);
        }
    }

    @Override
    public void setClickable(boolean clickable) {
        if (radioGroup != null) {
            radioGroup.setClickable(clickable);
        }
        super.setClickable(clickable);
    }
}
